import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.Dialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class TimeResultView extends ResultView implements IView
{
	private JDialog resultDialog;
	private TimeQuestion itsQuestion;
	
	public TimeResultView(TimeQuestion question, String userAnswer)
	{
		super(userAnswer);
		itsQuestion = question;
	}

	@Override
	public void display(Window parent)
	{
		boolean wrong = !userAnswer.equals(itsQuestion.getAnswer());
		
		resultDialog = new JDialog(parent, itsQuestion.getTitle(), Dialog.ModalityType.DOCUMENT_MODAL);
		// question label
		String s = new String(wrong ? "- Wrong!" : " - Correct!");
		JLabel textLabel = new JLabel(itsQuestion.getText() + s);
		textLabel.setForeground(wrong ? Color.red : new Color(0, 100, 0));
		textLabel.setBorder(BorderFactory.createEmptyBorder(30, 30, 30, 0));
		//JLabel emptyLabel = new JLabel("");
		FontManager.setComicFonts(textLabel);
		// your answer label
		JLabel userAnswerLabel = new JLabel("Your answer");
		// correct answer label
		JLabel correctAnswerLabel = new JLabel("Correct answer");
		// user answer panel
		ClockPanel clockUserAnswerPanel = new ClockPanel(new ClockGeometry());
		TimeConverter converter = new TimeConverter(userAnswer);
		clockUserAnswerPanel.setHours(converter.getHours());
		clockUserAnswerPanel.setMinutes(converter.getMinutes());
		// correct answer panel
		ClockPanel clockCorrectAnswerPanel = new ClockPanel(new ClockGeometry());
		converter = new TimeConverter(itsQuestion.getAnswer());
		clockCorrectAnswerPanel.setHours(converter.getHours());
		clockCorrectAnswerPanel.setMinutes(converter.getMinutes());
		// done button		
		JButton doneBtn = new JButton("Done");
		doneBtn.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				resultDialog.setVisible(false);
			}
		});
		// panel for everything except the button
		JPanel clocksPanel = new JPanel();
		GridLayout grid = new GridLayout(1, 1);
		clocksPanel.setLayout(grid);
		clocksPanel.add(clockUserAnswerPanel);
		clocksPanel.add(wrong ? clockCorrectAnswerPanel : new JLabel(""));
		// headers panel
		JPanel headersPanel = new JPanel();
		headersPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
		headersPanel.add(userAnswerLabel);
		headersPanel.add(Box.createRigidArea(new Dimension(150, 0)));
		headersPanel.add(wrong ? correctAnswerLabel : new JLabel(""));
		// center panel
		JPanel centerPanel = new JPanel();
		centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.PAGE_AXIS));
		centerPanel.add(headersPanel);
		centerPanel.add(clocksPanel);
		// done button panel
		JPanel doneBtnPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		doneBtnPanel.add(doneBtn);
		
		
		
		// populate the dialog
		resultDialog.add(textLabel, BorderLayout.NORTH);
		resultDialog.add(centerPanel, BorderLayout.CENTER);
		resultDialog.add(doneBtnPanel, BorderLayout.SOUTH);
		
		
		final Toolkit toolkit = Toolkit.getDefaultToolkit();
		final Dimension screenSize = toolkit.getScreenSize();
		final int x = (screenSize.width - resultDialog.getWidth()) / 2;
		final int y = (screenSize.height - resultDialog.getHeight()) / 2;
		resultDialog.setLocation(x, y);
		
		resultDialog.pack();
		resultDialog.setVisible(true);
	}

}
